package com.example.demo.config;

import com.example.demo.demoApi.NettyExecutor;
import com.example.demo.netty.NettyServer;
import com.example.demo.netty.socket.SocketServerHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:application-netty.properties")
public class NettyServerConfig {

    public static final String SOCKET_PREFIX = "netty.server.socket";

    @Autowired
    private NettyExecutor executor;

    @Bean(initMethod = "init", destroyMethod = "stop")
    @ConditionalOnProperty(prefix = SOCKET_PREFIX, value = "enabled", havingValue = "true")
    @ConfigurationProperties(SOCKET_PREFIX)
    public NettyServer socketServer(SocketServerHandler socketServerHandler) {
        return new NettyServer(socketServerHandler);
    }

    @Bean
    @ConditionalOnProperty(prefix = SOCKET_PREFIX, value = "enabled", havingValue = "true")
    public SocketServerHandler socketServerHandlerInitializer(){
        return new SocketServerHandler(executor);
    }
}
